home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6291 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  77 lines

  1. Path: news.crystalball.com!news
  2. From: Larry Weiss <lfw@oc.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question on string literals and char arrays
  5. Date: Fri, 23 Feb 1996 12:17:38 -0600
  6. Organization: crystalball.com
  7. Message-ID: <312E04C2.71E3@oc.com>
  8. References: <KEITHBAR.96Feb22192211@owl.WPI.EDU>
  9. NNTP-Posting-Host: external.oc.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14. CC: lfw@oc.com
  15.  
  16. Keith Barrett wrote:
  17.  > 
  18.  > Could someone explain to me in very great detail what is the
  19.  > difference between:  [see examples copied below]
  20.  
  21.  
  22. First example:
  23. ==============
  24.  main()
  25.  {
  26.    char *s;
  27.    s = "Keith";
  28.    printf("%s\n", s);
  29.  }
  30.  
  31.  allocated objects:
  32.  
  33.    o function named "main"
  34.    o char pointer named "s"
  35.    o a string containing "Keith"
  36.    o a string containing "%s\n"
  37.    o an unnamed char pointer to "%s\n" to assist the call to printf()
  38.  
  39.  semantics:
  40.  
  41.    o modify "s" to point to the string containing "Keith"
  42.    o call printf() with two arguments, the first a char pointer to "%s\n" that
  43.        is automatically allocated, and the second is the char pointer "s" 
  44.  
  45. Second example:
  46. ===============
  47. main()
  48.  {
  49.    char s[6];
  50.    s = "Keith";
  51.    printf("%s\n", s);
  52.  }
  53.  
  54.  allocated objects:
  55.  
  56.    o function named "main"
  57.    o a char named "s[0]"
  58.    o a char named "s[1]" allocated contiguous to s[0]
  59.    o a char named "s[2]" allocated contiguous to s[1]
  60.    o a char named "s[3]" allocated contiguous to s[2]
  61.    o a char named "s[4]" allocated contiguous to s[3]
  62.    o a char named "s[5]" allocated contiguous to s[4]
  63.    o a string containing "Keith"
  64.    o a string containing "%s\n"
  65.    o an unnamed char pointer to "%s\n" to assist the call to printf()
  66.    o an unnamed char pointer to s[0] to assist the call to printf()
  67.  
  68.  semantics:
  69.  
  70.    o modify a non-existant char pointer "s" to point to the string containing "Keith"
  71.    o call printf() with two arguments, the first a char pointer to "%s\n" that
  72.      is automatically allocated, and the second is a char pointer to s[0] 
  73.      that is automatically allocated 
  74.  
  75. The second example tries to manipulate a nonexistant char pointer named "s".
  76. This is the bug.
  77.